package LocalStorage

import (
	"KumquatDrivesServices/enums"
	"KumquatDrivesServices/utils"
	"fmt"
	"github.com/sirupsen/logrus"
	"os"
	"path/filepath"
)

func (LocalEngine) RemoveMergeFile(srcPath string) error {
	if !utils.PathExists(srcPath) {
		return fmt.Errorf("The path does not exist: %s", srcPath)
	}
	fileTypeEnums := enums.GetFileTypeBySuffix(filepath.Ext(srcPath))
	CoverPath := filepath.Join(filepath.Dir(srcPath), "cover", utils.GetBaseNameWithoutExt(srcPath)+".jpg")
	CoverPathBol := utils.PathExists(CoverPath)
	switch fileTypeEnums.Category {
	case enums.VIDEO:
		tsVideoPath := filepath.Join(filepath.Dir(srcPath), utils.GetBaseNameWithoutExt(srcPath))
		if CoverPathBol {
			if err := os.RemoveAll(tsVideoPath); err != nil {
				logrus.Errorf("Remove file %s failed", tsVideoPath)
			}
		}
	case enums.IMAGE:
		if CoverPathBol {
			if err := os.Remove(CoverPath); err != nil {
				logrus.Errorf("Remove file %s failed", CoverPath)
			}
		}
	default:
	}
	return os.Remove(srcPath)
}
